package com.azlogica.control;

import java.util.ArrayList;

import org.apache.commons.lang3.ArrayUtils;

import android.content.Context;
import android.database.Cursor;
import android.widget.Toast;

import com.azlogica.dao.FrameDaoAdapter;
import com.azlogica.entity.Frame;

public class ManageFrame {
	private FrameDaoAdapter frameDao;
	private static int FFFF = 65535;
	Context ctxt;

	public ManageFrame(Context context) {
		frameDao = new FrameDaoAdapter(context);
		frameDao.open();
		frameDao.close();
		ctxt = context;
	}

	public void createFrame(int header, int report, int sack, int messType,
			String imei, int status, int number, int accuracy, int speed,
			int azimuth, long altitude, long longitude, long latitude,
			String gpsUtc, int mcc, int mnc, int lac, int cllId, int battery,
			String sendTime) {
		frameDao.open();
		frameDao.createFrame(header, report, sack, messType, imei, status,
				number, accuracy, speed, azimuth, altitude, longitude,
				latitude, gpsUtc, mcc, mnc, lac, cllId, battery, sendTime);
		Cursor cursorF = frameDao.readLastFrame();
		if (cursorF.moveToFirst()) {
			int frameId = cursorF.getInt(cursorF.getColumnIndex("_id"));
			if (frameId > FFFF) {
				frameDao.updateFrame(frameId, frameId % FFFF);
				Toast.makeText(ctxt, "frameId" + frameId, Toast.LENGTH_SHORT)
						.show();
			} else {
				frameDao.updateFrame(frameId, frameId);
			}
		}
		frameDao.close();
	}

	public void updateFrame(int id, int send) {
		frameDao.open();
		frameDao.updateFrameSend(id, send);
		frameDao.close();
	}

	public ArrayList<Frame> readAllFrames() {
		frameDao.close();
		Cursor cursor = frameDao.readFrame();
		ArrayList<Frame> frames = new ArrayList<Frame>();
		if (cursor.moveToFirst()) {
			do {
				int id = cursor.getInt(cursor.getColumnIndex("_id"));
				int header = cursor.getInt(cursor
						.getColumnIndex("frame_header"));
				int report = cursor.getInt(cursor
						.getColumnIndex("frame_report"));
				int sack = cursor.getInt(cursor.getColumnIndex("frame_sack"));
				int mssType = cursor.getInt(cursor
						.getColumnIndex("frame_messtype"));
				String imei = cursor.getString(cursor
						.getColumnIndex("frame_imei"));
				int status = cursor.getInt(cursor
						.getColumnIndex("frame_status"));
				int number = cursor.getInt(cursor
						.getColumnIndex("frame_number"));
				int accuracy = cursor.getInt(cursor
						.getColumnIndex("frame_accuracy"));
				int speed = cursor.getInt(cursor.getColumnIndex("frame_spped"));
				int azimuth = cursor.getInt(cursor
						.getColumnIndex("frame_azimuth"));
				long altitude = cursor.getInt(cursor
						.getColumnIndex("frame_altitude"));
				long longitude = cursor.getInt(cursor
						.getColumnIndex("frame_longitude"));
				long latitude = cursor.getInt(cursor
						.getColumnIndex("frame_latitude"));
				String gpsUtc = cursor.getString(cursor
						.getColumnIndex("frame_gps_utc"));
				int mcc = cursor.getInt(cursor.getColumnIndex("frame_mcc"));
				int mnc = cursor.getInt(cursor.getColumnIndex("frame_mnc"));
				int lac = cursor.getInt(cursor.getColumnIndex("frame_lac"));
				int cllId = cursor.getInt(cursor.getColumnIndex("frame_cllId"));
				int battery = cursor.getInt(cursor
						.getColumnIndex("frame_battery"));
				String sendTime = cursor.getString(cursor
						.getColumnIndex("frame_sendTime"));
				int serial = cursor.getInt(cursor
						.getColumnIndex("frame_serial"));
				int send = cursor.getInt(cursor.getColumnIndex("frame_send"));
				frames.add(new Frame(id, header, report, sack, mssType, imei,
						status, number, accuracy, speed, azimuth, altitude,
						longitude, latitude, gpsUtc, mcc, mnc, lac, cllId,
						battery, sendTime, serial, send));
			} while (cursor.moveToNext());
		}
		cursor.close();
		frameDao.close();
		return frames;
	}
	
	public ArrayList<Frame> readAllUnsentFrames() {
		frameDao.close();
		Cursor cursor = frameDao.readUnsentFrames();
		ArrayList<Frame> frames = new ArrayList<Frame>();
		if (cursor.moveToFirst()) {
			do {
				int id = cursor.getInt(cursor.getColumnIndex("_id"));
				int header = cursor.getInt(cursor
						.getColumnIndex("frame_header"));
				int report = cursor.getInt(cursor
						.getColumnIndex("frame_report"));
				int sack = cursor.getInt(cursor.getColumnIndex("frame_sack"));
				int mssType = cursor.getInt(cursor
						.getColumnIndex("frame_messtype"));
				String imei = cursor.getString(cursor
						.getColumnIndex("frame_imei"));
				int status = cursor.getInt(cursor
						.getColumnIndex("frame_status"));
				int number = cursor.getInt(cursor
						.getColumnIndex("frame_number"));
				int accuracy = cursor.getInt(cursor
						.getColumnIndex("frame_accuracy"));
				int speed = cursor.getInt(cursor.getColumnIndex("frame_spped"));
				int azimuth = cursor.getInt(cursor
						.getColumnIndex("frame_azimuth"));
				long altitude = cursor.getInt(cursor
						.getColumnIndex("frame_altitude"));
				long longitude = cursor.getInt(cursor
						.getColumnIndex("frame_longitude"));
				long latitude = cursor.getInt(cursor
						.getColumnIndex("frame_latitude"));
				String gpsUtc = cursor.getString(cursor
						.getColumnIndex("frame_gps_utc"));
				int mcc = cursor.getInt(cursor.getColumnIndex("frame_mcc"));
				int mnc = cursor.getInt(cursor.getColumnIndex("frame_mnc"));
				int lac = cursor.getInt(cursor.getColumnIndex("frame_lac"));
				int cllId = cursor.getInt(cursor.getColumnIndex("frame_cllId"));
				int battery = cursor.getInt(cursor
						.getColumnIndex("frame_battery"));
				String sendTime = cursor.getString(cursor
						.getColumnIndex("frame_sendTime"));
				int serial = cursor.getInt(cursor
						.getColumnIndex("frame_serial"));
				int send = cursor.getInt(cursor.getColumnIndex("frame_send"));
				frames.add(new Frame(id, header, report, sack, mssType, imei,
						status, number, accuracy, speed, azimuth, altitude,
						longitude, latitude, gpsUtc, mcc, mnc, lac, cllId,
						battery, sendTime, serial, send));
			} while (cursor.moveToNext());
		}
		cursor.close();
		frameDao.close();
		return frames;
	}

	public byte[] buildFrame(Frame frame) {
		byte[] header = intToBytes(frame.getHeader(), 2);
		byte[] report = intToBytes(frame.getReport(), 2);
		byte[] sack = intToBytes(frame.getSack(), 1);
		byte[] messType = intToBytes(frame.getMessType(), 1);
		byte[] imei = frame.getImei().getBytes();
		byte[] status = intToBytes(frame.getStatus(), 1);
		byte[] number = intToBytes(frame.getNumber(), 1);
		byte[] accuracy = intToBytes(frame.getAccuracy(), 1);
		byte[] speed = intToBytes(frame.getSpeed(), 2);
		byte[] azimuth = intToBytes(frame.getAzimuth(), 2);
		byte[] altitude = intToBytes((int) frame.getAltitude(), 3);
		byte[] longitude = intToBytes((int) frame.getLongitude(), 4);
		byte[] latitude = intToBytes((int) frame.getLatitude(), 4);
		byte[] dateUTC;
		dateUTC = ArrayUtils.addAll(
				intToBytes(Integer.valueOf(frame.getGpsUTC().substring(0, 2)),
						1),
				intToBytes(Integer.valueOf(frame.getGpsUTC().substring(2, 4)),
						1));
		dateUTC = ArrayUtils.addAll(
				dateUTC,
				intToBytes(Integer.valueOf(frame.getGpsUTC().substring(4, 6)),
						1));
		dateUTC = ArrayUtils.addAll(
				dateUTC,
				intToBytes(Integer.valueOf(frame.getGpsUTC().substring(6, 8)),
						1));
		dateUTC = ArrayUtils.addAll(
				dateUTC,
				intToBytes(Integer.valueOf(frame.getGpsUTC().substring(8, 10)),
						1));
		dateUTC = ArrayUtils
				.addAll(dateUTC,
						intToBytes(
								Integer.valueOf(frame.getGpsUTC().substring(10,
										12)), 1));
		int mcc1 = Integer.parseInt("0"
				+ String.valueOf(frame.getMcc()).substring(0, 1), 16);
		int mcc2 = Integer.parseInt(
				String.valueOf(frame.getMcc()).substring(1, 3), 16);
		int mnc1 = Integer.parseInt("0"
				+ String.valueOf(frame.getMnc()).substring(0, 1), 16);
		int mnc2 = Integer.parseInt(
				String.valueOf(frame.getMnc()).substring(1, 3), 16);
		byte[] lac = intToBytes(frame.getLac(), 2);
		byte[] cllid = intToBytes(frame.getCllId(), 2);
		byte[] batt = intToBytes(frame.getBattery(), 1);
		byte[] sendTime;
		sendTime = ArrayUtils
				.addAll(intToBytes(
						Integer.valueOf(frame.getSendTime().substring(0, 2)), 1),
						intToBytes(
								Integer.valueOf(frame.getSendTime().substring(
										2, 4)), 1));
		sendTime = ArrayUtils
				.addAll(sendTime,
						intToBytes(
								Integer.valueOf(frame.getSendTime().substring(
										4, 6)), 1));
		sendTime = ArrayUtils
				.addAll(sendTime,
						intToBytes(
								Integer.valueOf(frame.getSendTime().substring(
										6, 8)), 1));
		sendTime = ArrayUtils.addAll(
				sendTime,
				intToBytes(
						Integer.valueOf(frame.getSendTime().substring(8, 10)),
						1));
		sendTime = ArrayUtils.addAll(
				sendTime,
				intToBytes(
						Integer.valueOf(frame.getSendTime().substring(10, 12)),
						1));
		byte[] serial = intToBytes(frame.getSerial(), 2);
		byte[] total;
		total = ArrayUtils.addAll(header, report);
		total = ArrayUtils.addAll(total, sack);
		total = ArrayUtils.addAll(total, messType);
		total = ArrayUtils.addAll(total, imei);
		total = ArrayUtils.addAll(total, status);
		total = ArrayUtils.addAll(total, number);
		total = ArrayUtils.addAll(total, accuracy);
		total = ArrayUtils.addAll(total, speed);
		total = ArrayUtils.addAll(total, azimuth);
		total = ArrayUtils.addAll(total, altitude);
		total = ArrayUtils.addAll(total, longitude);
		total = ArrayUtils.addAll(total, latitude);
		total = ArrayUtils.addAll(total, dateUTC);
		total = ArrayUtils.addAll(total, intToBytes(mcc1, 1));
		total = ArrayUtils.addAll(total, intToBytes(mcc2, 1));
		total = ArrayUtils.addAll(total, intToBytes(mnc1, 1));
		total = ArrayUtils.addAll(total, intToBytes(mnc2, 1));
		total = ArrayUtils.addAll(total, lac);
		total = ArrayUtils.addAll(total, cllid);
		total = ArrayUtils.addAll(total, batt);
		total = ArrayUtils.addAll(total, sendTime);
		total = ArrayUtils.addAll(total, serial);
		return total;
	}

	public static byte[] intToBytes(int x, int n) {
		byte[] bytes = new byte[n];
		for (int i = 0; i < n; i++, x >>>= 8)
			bytes[i] = (byte) (x & 0xFF);

		for (int i = 0; i < bytes.length / 2; i++) {
			byte temp = bytes[i];
			bytes[i] = bytes[bytes.length - 1 - i];
			bytes[bytes.length - 1 - i] = temp;
		}
		return bytes;
	}
}
